package com.devbobcorn.nekoration.client.event;

import com.devbobcorn.nekoration.client.rendering.entities.ElectroRenderer;
import com.mojang.blaze3d.matrix.MatrixStack;

import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.player.AbstractClientPlayerEntity;
import net.minecraft.client.entity.player.ClientPlayerEntity;
import net.minecraft.client.renderer.entity.EntityRenderer;
import net.minecraft.client.renderer.entity.IEntityRenderer;
import net.minecraft.client.renderer.entity.model.EntityModel;
import net.minecraft.entity.LivingEntity;
import net.minecraftforge.client.event.RenderHandEvent;
import net.minecraftforge.client.event.RenderPlayerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
public class PlayerRenderEvent {
    private static Minecraft mc = null;

    private static boolean ready = false;
    private static ElectroRenderer normal = null;
    private static ElectroRenderer slim = null;

    public static boolean hasElectro(String playerName){
        return playerName.equals("DevBobcorn");
    }

    @SubscribeEvent
    public static void onRenderPlayerPost(RenderPlayerEvent.Post event) {
        if (mc == null) mc = Minecraft.getInstance();
        AbstractClientPlayerEntity player = (AbstractClientPlayerEntity) event.getPlayer();
        if (!ready){
            initRenderers(mc.getEntityRenderDispatcher().getRenderer(player));
            return;
        }
        if (!hasElectro(player.getName().getContents()))
            return;
        if (player.getModelName().equals("slim"))
            slim.render(player, 0.0F, event.getPartialRenderTick(), event.getMatrixStack(), event.getBuffers(), event.getLight());
        else normal.render(player, 0.0F, event.getPartialRenderTick(), event.getMatrixStack(), event.getBuffers(), event.getLight());
    }

    @SubscribeEvent
    public static void onRenderHand(RenderHandEvent event){
        if (mc == null) mc = Minecraft.getInstance();
        ClientPlayerEntity player = mc.player;
        if (!ready){
            initRenderers(mc.getEntityRenderDispatcher().getRenderer(player));
            return;
        }
        if (!hasElectro(player.getName().getContents()))
            return;
        MatrixStack stack = event.getMatrixStack();
        stack.translate(0.0, 0.6, 0.0);
        if (player.getModelName().equals("slim"))
            slim.hand.render(event.getPartialTicks(), event.getMatrixStack(), event.getBuffers(), player, event.getLight());
        else normal.hand.render(event.getPartialTicks(), event.getMatrixStack(), event.getBuffers(), player, event.getLight());
        stack.translate(0.0, -0.6, 0.0);
    }

    @SuppressWarnings("unchecked")
    private static void initRenderers(EntityRenderer<? super ClientPlayerEntity> renderer){
        IEntityRenderer<LivingEntity, EntityModel<LivingEntity>> r = (IEntityRenderer<LivingEntity, EntityModel<LivingEntity>>)renderer;
        normal = new ElectroRenderer(r, false);
        slim = new ElectroRenderer(r, true);
        ready = true;
    }
}
